package cn.edu.ctbu.labtest.api;

import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.service.StudentService;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class StudentApiController {
/*controller是表现层，表现层要调用业务逻辑层StudentService*/
/*@Autowired:不需要亲自new，只需要自动包裹和自动注入就可以了*/
        @Autowired
        private StudentService studentService;

        @GetMapping("/student/list")
        public List<Student> list(HttpServletRequest httpServletRequest){

                HttpSession session=httpServletRequest.getSession();
                String username=(String) session.getAttribute("user_name");
                if (username==null||username.equals("")){
                        //无权读取

                        return null;
                }
                return studentService.findAll();
        }

        @PostMapping("/student/insert")
        public void insert(Student student){

                studentService.insert(student);
                log.info(student.toString());

        }
}
